#include <msp430g2553.h>
#include <string.h>
#include "uart.h"


#define LED1 BIT0
#define LED2 BIT6

//port2
#define RST BIT1
#define CS BIT0
#define RS BIT2

//port1
#define SDA BIT7
#define SCL BIT6

//input
#define ADCIn BIT3

#define p1on(x) {P1OUT |= x;}
#define p1off(x) {P1OUT &= ~x;}
#define p2on(x) {P2OUT |= x;}
#define p2off(x) {P2OUT &= ~x;}

volatile unsigned int i;
void delayd(unsigned int);
unsigned char TXData;
unsigned char TXByteCtr;

unsigned char orientation = 0;

int main(void)
{
	WDTCTL = WDTPW + WDTHOLD;		//stop watchdog timer
	
	BCSCTL1 = CALBC1_1MHZ;
	DCOCTL = CALDCO_1MHZ;
	BCSCTL2 = DIVS_2 + DIVM_0;
		
	UCB0CTL1 = UCSWRST;			//enable sw reset
	
	P1DIR |= LED1;
	P1OUT |= LED1;
	P2DIR |= CS + RS + RST;			
	P2OUT |= CS + RS + RST;
	P1SEL |= SDA + SCL;			//assign i2c
	P1SEL2 |= SDA + SCL;

	UCB0CTL0 = UCMST + UCMODE_3 + UCSYNC;	//i2c master, synch mode
	UCB0CTL1 = UCSSEL_2 + UCSWRST;		//use smclk, keep sw rst
	UCB0BR0 = 12;				//fSCL=SMCLK/12 = 100kHz
	UCB0BR1 = 0;

	UCB0I2CSA = 0X48;			//set slave address
	UCB0CTL1 &= ~UCSWRST;			//clear sw rst, resume op
	UCB0I2CIE |= UCSTTIE;			//en stt interrupt
	IE2 |= UCB0TXIE;			//enable tx interrupt
	
	TXData = 0x00;				//used to hold data
	
	p2off(CS + RST);
	p2on(CS + RST);
	
	initLCD();

	delayd(75);
	
	for(;;)
	{
		//write_command(0xff);
		p1off(LED1);
		delayd(65535);
		p1on(LED1);
		delayd(65535);

		_delay_cycles(1600000);

	}
}

void delayd(unsigned int d)
{
	volatile unsigned int i;
	for(i = 0; i < d; i++)
	{
		nop();
	}
}

